package controller

import (
	"github.com/gin-gonic/gin"
	"go_src/common"
	"go_src/dao"
)

func GetCategoryList(c *gin.Context) {

	category := dao.Category{}

	pagination := common.Pagination{Total: 1, TotalPage: 1, PerPage: 1, CurrentPage: 1}
	_ = c.ShouldBind(&pagination)
	_ = c.ShouldBind(&category)
	categoryResult := category.SelectCategories(&pagination)

	if categoryResult.Success {
		result := make(map[string]interface{})
		result["data"] = categoryResult.Data
		result["pagination"] = pagination
		c.JSON(200, gin.H{"message": "成功", "status": "success", "result": result})
	} else {
		c.JSON(200, gin.H{"message": "失败", "status": "error", "result": nil})
	}
}

func SaveCategory(c *gin.Context) {

	category := dao.NewCategory()

	_ = c.ShouldBind(&category)
	dbResult := category.SaveCategory()
	if dbResult.Success {
		c.JSON(200, gin.H{"message": "成功", "status": "success", "result": category})
	} else {
		c.JSON(200, gin.H{"message": "失败", "status": "error"})
	}

}
